package com.meessiom.train.crm.student.controller;


import com.meessiom.train.crm.student.model.Student;
import com.meessiom.train.crm.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/api/v1/students") //这是控制器的地址
public class StudentController {


    @Autowired
    //在服务器的控制器类上自动装配一个学生服务，用于完成与学生相关的业务操作
    private StudentService studentService;


    //控制器响应客户端的查询所有学生的方法
    @GetMapping
    //url = /api/v1/students/
    public List<Student> getAllStudents() {
        //控制器调用服务的方法去获得所有学生的列表
        return studentService.getAllStudent();
    }



    @GetMapping("{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable long id) {
        Student student = studentService.getStudentById(id);
        return ResponseEntity.ok(student);

    }



    @DeleteMapping("{id}")
    public ResponseEntity<HttpStatus> deleteStudent(@PathVariable long id) {
        studentService.deleteStudent(id);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


}
